package com.bee.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bee.auth.client.AuthHolder;
import com.bee.user.common.domain.RoleDO;
import com.bee.user.common.enums.RoleTypeEnum;
import com.bee.user.common.vo.RoleVO;
import com.bee.user.mapper.RoleMapper;
import com.bee.user.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 角色表(Role)表服务实现类
 *
 * @author makejava
 * @since 2019-08-27 10:19:14
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public RoleDO queryById(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public PageInfo<RoleVO> queryPage(RoleVO roleVO, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<RoleVO> list = roleMapper.queryAll(roleVO);
        return new PageInfo<>(list);
    }

    @Override
    public RoleDO insert(RoleDO domain) {
        domain.setOrgId(AuthHolder.getOrgId());
        domain.setEnable(Boolean.TRUE);
        domain.setType(RoleTypeEnum.ADD.ordinal());
        domain.setCreateTime(new Date());
        domain.setCreator(AuthHolder.getUser());
        domain.setUpdateTime(domain.getCreateTime());
        domain.setUpdator(domain.getCreator());
        roleMapper.insert(domain);
        return domain;
    }

    @Override
    public void update(RoleDO domain) {
        RoleDO updateDomain = new RoleDO();
        updateDomain.setId(domain.getId());
        updateDomain.setName(domain.getName());
        updateDomain.setEnable(domain.getEnable());
        updateDomain.setUpdator(AuthHolder.getUser());
        updateDomain.setUpdateTime(new Date());
        roleMapper.updateById(updateDomain);
    }

    @Override
    public boolean deleteById(Long id) {
        RoleDO roleDO = roleMapper.selectById(id);
        roleDO.setEnable(!roleDO.getEnable());
        return roleMapper.updateById(roleDO) > 0;
    }

    @Override
    public List<RoleDO> queryAllEnable() {
        RoleDO roleDO = new RoleDO();
        roleDO.setEnable(Boolean.TRUE);
        return roleMapper.selectList(Wrappers.query(roleDO));
    }
}